package org.jjche.file.modules.file.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.jjche.common.enums.FileType;
import org.jjche.common.exception.util.AssertUtil;
import org.jjche.core.util.FileUtil;
import org.jjche.file.conf.FileImageThumbnailProperties;
import org.jjche.file.conf.FileProperties;
import org.jjche.file.enums.FileErrorCodeEnum;
import org.jjche.file.modules.file.api.vo.FileVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

/**
 * <p>
 * 文件扩展服务
 * </p>
 *
 * @author miaoyj
 * @since 2024-07-11
 */
@Service
@RequiredArgsConstructor
public class FileExtService {
    private final FileStorageService fileStorageService;
    private final FileProperties fileProperties;

    /**
     * <p>
     * 上传图片
     * </p>
     *
     * @param file 文件
     * @return /
     */
    public FileVO uploadImage(MultipartFile file) {
        FileUtil.checkSize(fileProperties.getMaxSize(), file.getSize());
        String path = FileUtil.getBasePath();
        FileImageThumbnailProperties thumbnail = fileProperties.getThumbnail();
        FileInfo fileInfo = fileStorageService.of(file)
                .thumbnail(th -> th.size(thumbnail.getWidth(), thumbnail.getHeight()))
                .setPath(path)
                .upload();
        return getFileVO(fileInfo);
    }

    /**
     * <p>
     * 上传文件
     * </p>
     *
     * @param file 文件
     * @return /
     */
    public FileVO upload(MultipartFile file) {
        FileUtil.checkSize(fileProperties.getMaxSize(), file.getSize());
        String path = FileUtil.getBasePath();
        FileInfo fileInfo = fileStorageService.of(file)
                .setPath(path)
                .upload();
        return getFileVO(fileInfo);
    }

    /**
     * <p>
     * 转换fileVO
     * </p>
     *
     * @param fileInfo 文件
     * @return /
     */
    public static FileVO getFileVO(FileInfo fileInfo) {
        AssertUtil.notNull(fileInfo, FileErrorCodeEnum.FILE_UPLOAD_ERROR);
        String ext = fileInfo.getExt();
        FileType type = FileUtil.getFileType(ext);
        FileVO fileVO = new FileVO();
        fileVO.setId(NumberUtil.parseLong(fileInfo.getId()));
        fileVO.setUrl(fileInfo.getUrl());
        fileVO.setSize(fileInfo.getSize());
        fileVO.setFilename(fileInfo.getFilename());
        fileVO.setOriginalFilename(fileInfo.getOriginalFilename());
        fileVO.setExt(ext);
        fileVO.setThUrl(fileInfo.getThUrl());
        fileVO.setThFilename(fileInfo.getThFilename());
        fileVO.setThSize(fileInfo.getThSize());
        fileVO.setType(type);
        return fileVO;
    }

    /**
     * <p>
     * 删除文件
     * </p>
     *
     * @param filePaths 文件路径
     */
    public boolean delete(Set<String> filePaths) {
        if (CollUtil.isNotEmpty(filePaths)) {
            for (String filePath : filePaths) {
                fileStorageService.delete(filePath);
            }
        }
        return true;
    }
}
